<?php
class StoreFront {
	public $mSiteUrl;
	//Define the template  file for the page contents 
	public $mContentsCell='first_page_contents.tpl';
	//Define 
	public $mCategoriesCell='blank.tpl';
	//page title 
	public $mPageTitle;
	//paypal continue shopping link
	public $mPayPalContinueShoppingLink;
	 
	 
	
	
	//class constructor 
	public function __construct(){
		
		$this->mSiteUrl =Link::Build('');
		
	
	}
	
	//Initailize presentation object 
	public function init(){
		
		// create continue shooping link for paypal 
		if(!isset($_GET['AddProduct']))
		{
			/* store the current request neede for paypal 
			 * continue shopping functionality
			 */
			$_SESSION['paypal_continue_shopping']=Link::Build(str_replace(VIRTUAL_LOCATION,'',$_SERVER['REQUEST_URI']));
			$this->mPayPalContinueShoppingLink =$_SESSION['paypal_continue_shopping'];
		}else{
			//clean output buffer 
			ob_clean();
			$product_id =0;
			
			//Get the product id to be added to cart 
			if(isset($_GET['AddProduct']))
			 $product_id =(int)$_GET['AddProduct'];
			else 
			 trigger_error('AddProduct Not set');

			 $selected_attribute_groups =array();
			 $selected_attribute_values =array();
			 
			 //Get selected product attribute if any 
			 foreach($_POST as $key => $value)
			 {
			 	//if there are fields strings with attr_ in post array 
			 	if(substr($key, 0 ,5) == 'attr_')
			 	{
			 		// Get the selected attribute name and value 
			 		$selected_attribute_groups[]=  substr($key,strlen('attr_')) ;
			 		$selected_attribute_values[] =$_POST[$key];
			 	}
			 }
			 // Get product info 
			 $product =Catalog::GetProductDetails($product_id);
			 
			 //Build paypal url to add the product to cart 
			 $paypal_url =PAYPAL_URL .'?cmd=_cart&business='.PAYPAL_EMAIL .
			    '&item_name='.rawurlencode($product['name']);
			 
			 if(count($selected_attribute_groups) > 0)
			 $paypal_url .='&on0='.implode('/', $selected_attribute_groups) .
			               '&os0='.implode('/', $selected_attribute_values);
			 
			 $paypal_url .= '&amount='.($product['discounted_price']== 0 ?$product['price']:$product['discounted_price']).
			              '&currency_code='.PAYPAL_CURRENCY_CODE .'&add=1'.
			              '&shopping_url='.rawurlencode($_SESSION['paypal_continue_shopping']).
			              '&return='.rawurlencode(PAYPAL_RETURN_URL).
			              '&cancel_return='.rawurlencode(PAYPAL_CANCEL_RETURN_URL);
			 //Redirect to the paypal cart page 
			 header('HTTP/1.1 302 Found');
			 header('Location:'.$paypal_url);
			 
			 //clear the out put buffer and stop execution 
			 flush();
			 ob_flush();
			 ob_end_clean();
			 exit();
			 
			}
		
		
		
		//Load department details if visiting a department 
		if(isset($_GET['DepartmentId'])){
			$this->mContentsCell='department.tpl';
			$this->mCategoriesCell='categories_list.tpl';
		}elseif(isset($_GET['ProductId']) && isset($_SESSION['link_to_continue_shopping'])
		 && strpos($_SESSION['link_to_continue_shopping'], 'DepartmentId',0) !== false)
		 {
		   $this->mCategoriesCell='categories_list.tpl';
		 }
		 if(isset($_GET['ProductId']))
		 $this->mContentsCell ='product.tpl';
		 
		 // Load search result page if we are searching the catelog 
		 elseif (isset($_GET['SearchResults']))
		 $this->mContentsCell='search_results.tpl';
 		 
		 $this->mPageTitle = $this->_GetPageTitle();
	}
	
	// Returns page title 
	
	private function _GetPageTitle(){
		
		$page_title ='TshirtShop' .
		'Demo product Catelog from PHP and Mysql E-commerce ';
		
		if(isset($_GET['DepartmentId']) && isset($_GET['CategoryId'])){
			$page_title ='TshirtShop: ' . 
			Catalog::GetDepartmentName($_GET['DepartmentId']) . '-' .
			Catalog::GetCategoryName($_GET['CategoryId']);
			
			if(isset($_GET['Page']) && ((int)$_GET['Page'] >1)){
			$page_title .='- Page '. ((int)$_GET['Page']);
			}
		}elseif(isset($_GET['DepartmentId'])){
			$page_title ='TshirtShop: ' . 
			Catalog::GetDepartmentName($_GET['DepartmentId']);
			
		   if(isset($_GET['Page']) && ((int)$_GET['Page'] >1)){
			$page_title .='- Page '. ((int)$_GET['Page']);
			}
		}elseif(isset($_GET['ProductId'])){
		    $page_title ='TshirtShop: ' . 
			Catalog::GetProductName($_GET['ProductId']);
			
		}elseif(isset($_GET['SearchResults']))
		{
			$page_title ='TshirtShop: "';
            
			//Display the search string
            $page_title .= trim(str_replace('-', '', $_GET['SearchString'])) .'"(';
            
            //Display all words search  or any word search
            $all_words = isset($_GET['AllWords']) ? $_GET['AllWords'] : 'off' ;
            
            $page_title .= (($all_words == 'on')) ? 'all' :'any' . ')'.'-words search' ;
            
            //Display Page number 
            if(isset($_GET['Page']) && ((int)$_GET['Page']) >1 )
            $page_title .= ',page '.((int)$_GET['Page']);
            
            $page_title .= ')';
            
			
		}else{
			
		   if(isset($_GET['Page']) && ((int)$_GET['Page'] >1)){
			$page_title .='- Page '. ((int)$_GET['Page']);
			}
		}
		return $page_title;
		
	}
	
}